﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Linq;

namespace FileApplication.UNEF
{
    public partial class DetailForm : Form
    {
        /// <summary>
        /// 原始行信息
        /// </summary>
        private DataRow dataRow;
        /// <summary>
        /// 当前页 数据源 有且只有一条记录
        /// </summary>
        private DataTable dataTable;

        public DetailForm()
        {
            InitializeComponent();
        }

        public DetailForm(DataRow _dataRow):this()
        {
            this.dataRow = _dataRow;
            DataTable sourceTable = _dataRow.Table;
            dataTable = new DataTable();
            dataTable.TableName = sourceTable.TableName;
            DataColumnCollection columns = sourceTable.Columns;
            for (int i = 0; i < columns.Count; i++)
            {
                DataColumn dataColumn = new DataColumn() { ColumnName = columns[i].ColumnName, DataType = columns[i].DataType, DefaultValue = columns[i].DefaultValue };
                dataTable.Columns.Add(dataColumn);//设置相同的列数据
            }
            dataTable.Rows.Add(_dataRow.ItemArray);

        }

        private void FormDetail_Load(object sender, EventArgs e)
        {
            //https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.forms.bindingcontext?view=netframework-4.6
            Control.ControlCollection controls = this.panel1.Controls;
            string suffix = "_input";
            foreach (Control item in controls)
            {
                string name = item.Name.Replace(suffix,"");
                if (dataTable.Columns.Contains(name))
                {
                    try
                    {
                        string propName = GetBindProp(item.GetType());                        ;
                        Binding binding = new Binding(propName, dataTable, item.Name);
                        item.DataBindings.Add(binding);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }                    
                }
            }
        }


        /// <summary>
        /// 设置常用的值
        /// </summary>
        /// <returns></returns>
        public Dictionary<Type,string> GetBindPropDict()
        {
            Dictionary<Type, string> keyValues = new Dictionary<Type, string>();
            keyValues.Add(typeof(TextBox), "Text");
            keyValues.Add(typeof(RichTextBox), "Text");
            keyValues.Add(typeof(NumericUpDown), "Value");
            keyValues.Add(typeof(ComboBox), "Text");
            keyValues.Add(typeof(DateTimePicker), "Value");
            return keyValues;
        }

        /// <summary>
        /// 获取匹配的属性
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetBindProp(Type type)
        {
            Dictionary<Type, string> keyValues = GetBindPropDict();

            if (keyValues.ContainsKey(type))
            {
                return keyValues[type];
            }

            return "Text";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("保存成功");
            //修改
            if (dataRow.Table.Rows.IndexOf(dataRow) >= 0)
            {
                dataRow.ItemArray = dataTable.Rows[0].ItemArray;
                dataRow.AcceptChanges();
            }
            else//新增
            {
                dataRow.Table.Rows.Add(dataTable.Rows[0].ItemArray);//将当前行加入Table
                //dataRow.AcceptChanges();
            }
           
            this.Close();
        }
    }
}
